#!/bin/bash

# Script to backup data. Does a weekly full and daily incremental

SERVERNAME="caja17.fis.usal.es"
DATA="/usr/local/Plone"
DATA_FS_PATH="/usr/local/Plone/zinstance/var/filestorage/Data.fs"
DATA_EXCLUDE="*.mp9 ${DATA_FS_PATH}" # Data.fs is being backed up on its own. *.mp9 is a dummy wildcard so that it is never empty
LIST="/tmp/backlist_$$.txt"
LIST2="/tmp/backups_$$.txt" # a precaution - don't want to mistakenly delete all the backed up files later!!!!
DESTINATION="/home/pablin/Escritorio/Nueva" #"/usr/local/Plone/Backups"
RETENTION=21 #days of backup files including full and incrementals
FULLBACKUPDAY="Thu" # ddd format. Thursday morning seems like the most 'non' day as there could be downtime

# This DBBACKUP is the command line used to back up the database. I will always use a full backup every day
DBBACKUP="/usr/local/Plone/zinstance/bin/repozo -BvzQ -r ${DESTINATION} -f ${DATA_FS_PATH}"
PYTHONPATH=/usr/local/Plone/Zope-2.10.7-final-py2.4/lib/python
export PYTHONPATH # otherwise repozo.py will probably not be able to find its libraries

logger "Starting backup for ${SERVERNAME}" # almacena los mensajes de log en /var/log/messages
echo "Starting backup for \"${SERVERNAME}\""

#
# We need to check how many incrementals we've had since the last full backup, and we
# will tar everything since the last full backup. So the day after a full, last.txt will have
# a value of 1, so we'll back up the last 1 x 24hrs of files. So incrementals get bigger and bigger
# each day. This is a safe approach that doesn't require you to apply all incrementals since the 
# last full backup. So the day before a full backup, there will be 6 days of data in the incremental.
#
if [ -f "${DESTINATION}/last.txt" ]
then
    echo "${DESTINATION}/last.txt file exist" > /dev/null
else   # There is no last.txt, so we'll create one with a value of 7 (just to be safe)
    echo "Sorry, ${DESTINATION}/last.txt file does not exist" > /dev/null
    touch "${DESTINATION}/last.txt"
    echo "7" > "${DESTINATION}/last.txt" # a safe default value of 7
fi
Cnt=`cat ${DESTINATION}/last.txt`
#
set $(date) # date parms $1 ddd, $2 mmm, $3 dd, $4 hh:mm:ss, $5 NZDT, $6 yyyy 
if test "$1" = $FULLBACKUPDAY ; then
#############################################################
#
# weekly a full backup of all data:
#
#############################################################
# First tar the data files
#

     tar cfz "${DESTINATION}/data_full_$6-$2-$3.tgz" $DATA --exclude $DATA_EXCLUDE
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
              logger "Potential problem in backup of ${SERVERNAME}" $RESULT
          echo "tar returned result code of " $RESULT 
      fi
      "backup: Finished tarring data files..." > "${DESTINATION}/log.txt"
# then run the database backup script
        `${DBBACKUP}`
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
              logger "Potential problem in backup of ${SERVERNAME}" $RESULT
          echo "DB Backup script returned result code of " $RESULT
      fi
      logger "backup: Finished backing up database..."
       

# Reset the counter of days since last full backup
#
     echo "1" > "${DESTINATION}/last.txt" 

else
#############################################################
#
# incremental backup:
#
#############################################################
# First tar the data files
#
       
        find $DATA -depth -type f \( -ctime -${Cnt} -o -mtime -${Cnt} \) -print > $LIST
        tar cfzT "${DESTINATION}/data_diff_$6-$2-$3.tgz" "$LIST" --exclude $DATA_EXCLUDE
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
              logger "Potential problem in backup of ${SERVERNAME}" $RESULT
          echo "tar produced this " $RESULT
        fi
        logger "backup: Finished tarring data files..."
        `${DBBACKUP}`
        if [ $RESULT -ne 0 ] ; then
              logger "Potential problem in backup of ${SERVERNAME}" $RESULT
          echo "Backup script produced this " $RESULT
        fi
        logger "backup: Finished backing up database..."

        rm -f "$LIST"

#
# Increment the counter of days since last full backup and write to last.txt
#
     echo `expr $Cnt + 1` > "${DESTINATION}/last.txt" 
fi


#############################################################
#
# Clean up cycle...
# Will find all backup files older than the retention period, then delete them
# 
#############################################################

find $DESTINATION -type f -ctime +${RETENTION} -print  > $LIST2 # create the list
xargs rm -f < $LIST2 # delete the files
echo "Removing: " `cat $LIST2` # prints the list of files deleted to stdout
rm -f "$LIST2" # delete the temp list

logger "backup: Backup for ${SERVERNAME} is complete"
echo "backup: Backup for \"${SERVERNAME}\" is complete"
#############################################################

exit 0
